package storage

import (
	"context"
	"fmt"
	"sync"

	"enzyme-backend/core/models"
	"enzyme-backend/core/utils"

	G "enzyme-backend/core/generated"
)

var (
	// GlobalOrderType 全局OrderType存储实例
	GlobalOrderType *OrderTypeMemoryStorage
	orderTypeOnce   sync.Once
)

// OrderTypeStorage OrderType存储实现
type OrderTypeMemoryStorage struct {
	orderTypes map[string]models.OrderType
}

func init() {
	orderTypeOnce.Do(func() {
		GlobalOrderType = &OrderTypeMemoryStorage{
			orderTypes: G.ValidOrders,
		}
		utils.GlobalLogger.Info("OrderType存储已初始化")
	})
}

// GetOrderType 获取指定订单类型
func (ots *OrderTypeMemoryStorage) GetOrderTypeByName(ctx context.Context, orderTypeKey string, orderType *models.OrderType) error {
	ot, exists := ots.orderTypes[orderTypeKey]
	if !exists {
		return fmt.Errorf("订单类型不存在")
	}
	*orderType = ot
	return nil
}

// GetAllOrderTypes 获取所有订单类型
func (ots *OrderTypeMemoryStorage) GetOrderTypes(ctx context.Context) map[string]models.OrderType {
	return ots.orderTypes
}
